package com.leyou.user.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PublicKey;


@Data
@Slf4j
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {

    private String pubKeyPath; // 公钥地址
    // 公钥
    private PublicKey publicKey;

    private AppTokenProperties app = new AppTokenProperties();

    @Data
    public static class AppTokenProperties{
        private Long id;   // 当前微服务的自己的id
        private String secret;// 当前微服务的密码
        private String headerName; // 请求头的名称
    }

    /**
     * 当对象初始化完成之后，才去调用的
     * 当JwtProperties对象创建完成之后，我们让这个类加载公钥和私钥
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        try{
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
            log.info("【网关】初始化公钥成功！");
        } catch (Exception e) {
            log.error("【网关】初始化公钥失败！", e);
            throw new RuntimeException(e);
        }
    }
}